import datetime

from google.appengine.api import users
from google.appengine.api import datastore
from google.appengine.api import mail

import tvdb
import tvweb

class RPCMethods:
    
    def ValidateDate(self, *args):
        try:
            date = datetime.date(args[2], args[1]+1, args[0])
            dateServer = datetime.date.today()
            if date != dateServer:
                result = tvdb.restore_calendar_by_date(users.get_current_user(), date)
                cals = {}
                for i in range(result.count()):
                    cals[str(i)] = dict(serie=result[i].serie, season=result[i].season, 
                        nroEp=result[i].nroEp, episode=result[i].episode, date=str(result[i].date))
                return cals
        except:
            pass
        return ''
    
    def AddSerie(self, *args):
        try:
            user = users.get_current_user()
            val = tvdb.store_serie(str(args[0]).lower(), user)
            return args[0] if val else ''
        except Exception, reason:
            return ''
            
    def RemoveSerie(self, *args):
        try:
            user = users.get_current_user()
            tvdb.remove_serie(user, args[0])
            result = tvdb.restore_series(user)
            series = {}
            for i in range(len(result)):
                series[str(i)] = dict(serie=result[i])
            return series
        except:
            return ''
            
    def CheckNextEpisode(self, *args):
        try:
            user = users.get_current_user()
            result = tvdb.check_next_episode(user, args[0])
            return dict(serie=result[0].serie.upper(), season=result[0].season, 
                    nroEp=result[0].nroEp, episode=result[0].episode, date=str(result[0].date))
        except:
            return ''
            
    def SelectByDate(self, *args):
        try:
            dateSplit = args[0].split('/')
            date = datetime.date(int(dateSplit[2]), int(dateSplit[0]), int(dateSplit[1]))
            user = users.get_current_user()
            result = tvdb.restore_calendar_by_date(users.get_current_user(), date)
            cals = {}
            for i in range(result.count()):
                cals[str(i)] = dict(serie=result[i].serie, season=result[i].season, 
                    nroEp=result[i].nroEp, episode=result[i].episode, date=str(result[i].date))
            return cals
        except:
            return ''

    def UpdateTodayEpisodes(self, *args):
        try:
            date = datetime.date(args[2], args[1]+1, args[0])
            dateServer = datetime.date.today()
            user = users.get_current_user()
            today = None
            if date != dateServer:
                today = tvdb.restore_calendar_by_date(users.get_current_user(), date)
            else:
                today = tvdb.search_today_episodes(users.get_current_user())
            cals = {}
            for i in range(today.count()):
                cals[str(i)] = dict(serie=today[i].serie, season=today[i].season, 
                    nroEp=today[i].nroEp, episode=today[i].episode, date=str(today[i].date))
            return cals
        except:
            return ''
            
    def ObtainCalendarBySerie(self, *args):
        try:
            user = users.get_current_user()
            calendars = tvdb.restore_calendar(users.get_current_user(), args[0])
            cals = {}
            for i in range(calendars.count()):
                cals[str(i)] = dict(serie=calendars[i].serie, season=calendars[i].season, 
                    nroEp=calendars[i].nroEp, episode=calendars[i].episode, date=str(calendars[i].date))
            return cals
        except:
            return ''
            
    def SendBugReportMail(self, *args):
        try:
            user = users.get_current_user()
            mailFrom = user.nickname() + "@gmail.com"
            mailTo = "diego.sarmentero@gmail.com"
            subject = "PyTv Bug Report"
            body = args[0]
            mail.send_mail(mailFrom, mailTo, subject, body)
        except:
            pass
            
    def SavePreferences(self, *args):
        try:
            user = users.get_current_user()
            tvdb.save_preferences(user, args[0], args[1], args[2], args[3])
        except:
            pass

    def ObtainDownloads(self, *args):
        try:
            user = users.get_current_user()
            links = tvweb.obtain_downloads(user)
            downloads = {}
            for i in range(len(links)):
                d = links[i]
                downloads[str(i)] = dict(serie=d.serie, links=d.links, source=d.source)
            return downloads
        except:
            return ''

    def SaveDesign(self, *args):
        try:
            user = users.get_current_user()
            tvdb.store_design(user, args[0], args[1])
        except:
            pass
